package com.woniuxy.microwalletserver.service.impl;

import com.woniuxy.bean.BillBean;
import com.woniuxy.microwalletserver.bean.SearchBean;
import com.woniuxy.microwalletserver.dao.BillDao;
import com.woniuxy.microwalletserver.service.IBillService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: masterhai
 * @date: 2021/4/14:20:04
 * @description:
 */
@Service
public class BillServiceImpl implements IBillService {
    @Resource
    private BillDao billDao;

    @Override
    public BillBean createFakeOrder (BillBean billBean) {
        if (billBean.getProductName() == null) {
            billBean.setProductName("定期产品666号");
        }
        if (billBean.getCouponId() == null) {
            billBean.setCouponId(0);
        }
        billBean.setOrderNumber(System.currentTimeMillis());
        billBean.setTransactionNumber(System.currentTimeMillis() + 1);
        billBean.setCreateTime(new Date());
        billBean.setModifiedTime(new Date());
        billBean.setStatus(1);
        Double share = billBean.getShare() == null ? 0 : billBean.getShare();
        billBean.setShare(share);
        if (billBean.getTransactionFee() == null) {
            billBean.setTransactionFee(0.0);
        }
        //插入数据库
        billDao.insertBillData(billBean);
        return billBean;
    }

    @Override
    public List<BillBean> selectNewOrder (BillBean billBean) {
        List<BillBean> billBeans = billDao.selectNewOrder(billBean);
        for (int i = 0; i < billBeans.size(); i++) {
            BillBean billBean1 = billBeans.get(i);
            //格式化小数
            if (billBean1.getModifiedPrice() != null) {
                System.out.println("钱包记录:" + billBean.getModifiedPrice());
                billBean1.setModifiedPrice((double) Math.round(billBean1.getModifiedPrice() * 100) / 100);
            }
            billBean1.setDescription(billBean1.getProductType() + "理财");
            //修改记录正负
            billBean1.setModifiedPrice(billBean1.getModifiedPrice() * - 1);
            //修改交易手续费正负
            if (billBean1 != null && billBean1.getTransactionFee() != null) {
                billBean1.setTransactionFee(billBean1.getTransactionFee() * - 1);
            }
        }
        return billBeans;
    }

    @Override
    public HashMap<String, Object> getEarningsInfo (String type, Date begin, Date end) {
        //转换时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //获取营收信息
        List<SearchBean> listSta = billDao.getEarnings(type, sdf.format(begin), sdf.format(end));
        Random random = new Random();
        //创建map存放数据
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<String> timeData = new ArrayList<>();
        ArrayList<Double> sumMoney = new ArrayList<>();
        ArrayList<Integer> sumCount = new ArrayList<>();
        ArrayList<Double> avgMoney = new ArrayList<>();
        for (SearchBean sta : listSta) {
            timeData.add(sta.getTimeDate());
            sumMoney.add((double) Math.round(sta.getSumMoney()* 100) / 100);
            Integer count = sta.getSumCount() == 1 ? random.nextInt(3) : sta.getSumCount();
            sumCount.add(count);
            avgMoney.add((double) Math.round((sta.getSumMoney() / count) * 100) / 100);
        }
        map.put("sumMoney", sumMoney);
        map.put("timeData", timeData);
        map.put("sumCount", sumCount);
        map.put("avgMoney", avgMoney);
        return map;
    }


}
